package com.pan.structual.flyweight.utils;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;


/**
 * 这算享元模式吧
 */
public enum XmlUtils {
    INSTANCE;

    Document xmlDoc;
    HashMap<String,Object> objectPool;


    {
        objectPool = new HashMap<>();
        try {
            xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                    .parse(new File("src/main/java/com/pan/structual/flyweight/config.xml"));
            xmlDoc.normalizeDocument();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public int[] getPictureWidthAndHeight(){
        String key = "pictureWH";
        if (objectPool.containsKey(key)) {
            return (int[]) objectPool.get(key);
        }
        NodeList pictureList = xmlDoc.getDocumentElement().getElementsByTagName("picture");
        Element pictureElement = (Element) pictureList.item(0);
        String widthStr = pictureElement.getElementsByTagName("width").item(0).getTextContent().strip();
        int width = Integer.parseInt(widthStr);
        String heightStr = pictureElement.getElementsByTagName("height").item(0).getTextContent().strip();
        int height = Integer.parseInt(heightStr);
        int[] wh = new int[]{width,height};
        objectPool.put(key,wh);
        return wh;
    }

    public Font getFont() {
        String key = "font";
        if (objectPool.containsKey(key)) {
            return (Font) objectPool.get(key);
        }
        NodeList fontList = xmlDoc.getDocumentElement().getElementsByTagName("font");
        Element fontElement = (Element) fontList.item(0);
//        String fontSizeStr = fontElement.getElementsByTagName("fontSize").item(0).getTextContent().strip();
//        int fontSize = Integer.parseInt(fontSizeStr);
//        String fontStyleStr = fontElement.getElementsByTagName("fontStyle").item(0).getTextContent().strip();
//        int fontStyle = Integer.parseInt(fontStyleStr);

        int fontSize = getFontSizeAndStyle("Size");
        int fontStyle = getFontSizeAndStyle("Style");
        String fontName = fontElement.getElementsByTagName("fontName").item(0).getTextContent().strip();
        Font font = new Font(fontName, fontStyle, fontSize);
        objectPool.put(key, font);
        return font;
    }

    public int getFontSizeAndStyle(String sizeOrStyle){
        NodeList fontList = xmlDoc.getDocumentElement().getElementsByTagName("font");
        Element fontElement = (Element) fontList.item(0);
        String fontSizeStr = fontElement.getElementsByTagName("font"+sizeOrStyle).item(0).getTextContent().strip();
        int fontSize = Integer.parseInt(fontSizeStr);
        return fontSize;
    }


    public Color getFontColor(){
        String key = "fontColor";
        if (objectPool.containsKey(key)) {
            return (Color) objectPool.get(key);
        }
        NodeList fontColorList = xmlDoc.getDocumentElement().getElementsByTagName("fontColor");
        Element fontColorElement = (Element) fontColorList.item(0);
        String redStr = fontColorElement.getElementsByTagName("red").item(0).getTextContent().strip();
        int red = Integer.parseInt(redStr);
        String greenStr = fontColorElement.getElementsByTagName("green").item(0).getTextContent().strip();
        int green = Integer.parseInt(greenStr);
        String blueStr = fontColorElement.getElementsByTagName("green").item(0).getTextContent().strip();
        int blue = Integer.parseInt(blueStr);
        Color color = new Color(red, green, blue);
        objectPool.put(key,color);
        return color;
    }
    public Color getBackgroundColor(){
        String key = "backgroundColor";
        if (objectPool.containsKey(key)) {
            return (Color) objectPool.get(key);
        }
        NodeList fontColorList = xmlDoc.getDocumentElement().getElementsByTagName("backgroundColor");
        Element fontColorElement = (Element) fontColorList.item(0);
        String redStr = fontColorElement.getElementsByTagName("red").item(0).getTextContent().strip();
        int red = Integer.parseInt(redStr);
        String greenStr = fontColorElement.getElementsByTagName("green").item(0).getTextContent().strip();
        int green = Integer.parseInt(greenStr);
        String blueStr = fontColorElement.getElementsByTagName("green").item(0).getTextContent().strip();
        int blue = Integer.parseInt(blueStr);
        Color color = new Color(red, green, blue);
        objectPool.put(key,color);
        return color;
    }

}
